require(['./config'], () => {
    require(['header','jquery','cookie'], () => {
      class Register {
        constructor () {
          this.userReg()
        }
        userReg () {
            $('#registerBtn').on('click',function(){
                const name = $('#userName').val()
                const pwd = $('#userPwd').val()
                var userInfo = { name, pwd }
                var userList = $.cookie('userInfo')
                if(name != "" && pwd != ""){
                    if(userList){
                        const isExist = JSON.parse(userList).some(user => user.name == name)
                        if(isExist){
                            alert("用户名已被注册，请重新填写")
                            $('#userName').val("")
                            $('#userPwd').val("")
                        }else{
                            userList = JSON.parse(userList)
                            userList.push(userInfo)
                            $.cookie('userInfo', JSON.stringify(userList),{ expires: 7 })
                            alert('注册成功，即将跳转登录页')
                            window.open('/html/login.html', '_self')
                        }
                    }else{
                        // 没有数据
                        var arr = [ userInfo ]
                        $.cookie('userInfo', JSON.stringify(arr))
                        alert('注册成功，即将跳转登录页')
                        window.open('/html/login.html', '_self')
                    }
                }else{
                    alert("用户名及密码不能为空！")
                }
            })
        }
      }
      new Register()
    })
  })